<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- Individual YUI CSS files -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/reset-fonts-grids/reset-fonts-grids.css">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/resize/assets/skins/sam/resize.css">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.6.0/build/layout/assets/skins/sam/layout.css">
<!-- Individual YUI JS files -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/animation/animation-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/dragdrop/dragdrop-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/history/history-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/resize/resize-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/layout/layout-min.js"></script>

<!-- Application Modules -->
<script type="text/javascript" src="EventManager.js"></script>
<script type="text/javascript" src="RecordsetManager.js"></script>
<script type="text/javascript" src="UIManager.js"></script>

<script type="text/javascript">

var App = function(){
	//private members
    var _className = "Application";  // I know this is a singleton and the application object, but hey it matches the pattern.
	var _modLoadCount = 0;
	function _init(){
			that.em = new EventManager();
			//  setup events before loading the other modules so they can subscribe
			that.em.addEvent("AppInit", that);    // first phase of initialization is complete.
			that.em.addEvent("ModuleLoaded", that);  // this is fired by each module once loaded
			that.em.subscribe("ModuleLoaded",_onModuleLoaded,that);
			that.em.addEvent("AppLoaded", that);  // this is fired once all the modules have loaded
			that.uim = new UIManager(that.em);
			that.rm = new RecordsetManager(that.em);
			that.em.fire("AppInit", that);
	};

	function _onModuleLoaded( eventName, params, obj) {
		switch(params[0]){
			case 'rm' :
				that.rm.loaded = true;
				_modLoadCount += 1;
				break;
			case 'uim':
				that.uim.loaded = true;
				_modLoadCount += 2;
		}
		if (_modLoadCount == 3) {
				that.em.fire("AppLoaded",that);
		}
	};


	//public members
	var that = {
			name : "App",
            getClass: function() { return _className;},
            isA: function(strClass){ if(strClass === _className) return true; return false;}
	};

	_init();
	return that;
}();  //singleton so make this a call to instantiate it.


function UIManager(em){
	//private memebers
    var _className = "UIManager";

    function _init() {
			em.addEvent("UILoaded", that);  // this is fired by uim when teh ui is loaded or re-loaded
			em.addEvent("NewLayout", that);  // this is fired by other modules to request a layout change
			em.subscribe("AppInit",_onAppInit,that);
			em.subscribe("AppLoaded",_onAppLoaded,that);
	};

	function _onAppInit( eventName, params, obj) {
				// this event is fired by App when all the modules have been created  and have registered their events
				// we can now subscribe to any we want to receive
				em.subscribe("NewLayout",_onNewLayout,that);
				em.fire("ModuleLoaded",that.name);
	};

	function _onAppLoaded( eventName, params, obj) {
				// this event is fired by App when all the modules have been loaded
				// we can now start running the app
				console.debug(" UIM received AppLoaded ");

	};

	function _onNewLayout(eventName, params, obj) {
				//someone has signaled a new layout parse the params and update the UI
				console.debug(" UIM received NewLayout ");
	};

	//public members
	var that = {
		name : "uim",
        getClass: function() { return _className;},
        isA: function(strClass){ if(strClass === _className) return true; return false;}
	};

	_init();
	return that;
}

</script>


</head>
<body>
<input type="button" value="New Query" name="New Query" onclick="App.em.fire('NewQuery',App);"> </input>
<input type="button" value="New Layout" name="New Layout" onclick="App.em.fire('NewLayout',App);"> </input>

</body>
</html>
